import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { ActivatedRoute } from '@angular/router';
import { CommonService } from '../../../services-common/common.service';
import { defalutImg } from '../../../comm';
import { PromotionShareService } from '../promotion-share.service';

@Component({
  selector: 'app-divide-developer-details',
  templateUrl: './divide-developer-details.component.html',
  styleUrls: ['./divide-developer-details.component.scss']
})
export class DivideDeveloperDetailsComponent implements OnInit {

  fallback: string = defalutImg;
  routeParams: any = {};
  detailsData: any = {};

  /**
   * 审核 Modal
   * ----------------------------------------------------------------
   */
  isExamineVisible = false;
  auditOpinion: string = '';

  constructor(
    private msg: NzMessageService,
    private activatedRoute: ActivatedRoute,
    private commonService: CommonService,
    private services: PromotionShareService
  ) {
    activatedRoute.queryParams.subscribe((res: any) => {
      this.routeParams = res || {};
    })
  }

  ngOnInit(): void {
    this.loadDetailsData();
  }

  async loadDetailsData() {
    let params = {
      id: this.routeParams.id
    }
    const msgId = this.msg.loading('数据加载中..', { nzDuration: 0 }).messageId;
    const {code, data, message}: any = await this.services.getDeveloperDetailsData(params);
    this.msg.remove(msgId);

    if (code === 0) {
      this.detailsData = data || {};
    } else {
      this.msg.error(message);
    }
  }

  /**
   * ----------------------------------------------------------------
   * 审核
   * ----------------------------------------------------------------
   */
   showExamineModal() {
    this.auditOpinion = '';
    this.isExamineVisible = true;
  }

  async subExamine(type: number) {
    if (type === 3 && this.commonService.isEmpty(this.auditOpinion)) {
      this.msg.warning('请输入审核意见');
      return
    }

    let params = {
      id: this.routeParams.id,
      status: type,
      auditNote: this.auditOpinion
    }
    const res: any = await this.services.auditDeveloperData(params);

    if (res.code === 0) {
      this.isExamineVisible = false;
      this.msg.success('操作成功');
      this.loadDetailsData();
    } else {
      this.msg.error(res.message);
    }
  }

  pageBack() {
    history.back();
  }
}
